<template>
  <div>
    <div class="action-bar">
      <!-- <el-button size="mini" type="primary" icon="el-icon-upload2" @click="uploadDialogFormVisible=true">导入</el-button> -->
      <el-button size="mini" icon="el-icon-download" @click="exportExcel">导出</el-button>
      <el-button v-role="['worker']" type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
    </div>
    <FormDialog title="新建" class="fixed-dialog-height" :show="dialogFormVisible" width="60%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" ref="editForm" />
    </FormDialog>
    <FormDialog title="导入" :show="uploadDialogFormVisible" width="40%" @on-ok="closeDialog" @on-cancel="closeDialog">
      <UploadNavigation :download-funct="template" file-name="移交服务周报导入模板" :upload-error="handleError" :action="importExcelUrl" :upload-success="handleSuccess" />
    </FormDialog>
  </div>
</template>
<script>
import UploadNavigation from '@neu/biz-core/components/UploadNavigation'
import FormDialog from '@neu/biz-core/components/FormDialog'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
import { getToken } from '@neu/biz-core/utils/auth'
import { exportExcel, template, importExcelUrl } from '@neu/dcom-ui/api/transferWeekly'
import { formatDate } from '@neu/biz-core/utils/utils'
export default {
  name: 'TransferWeeklyActionBar',
  components: {
    FormDialog,
    EditForm,
    UploadNavigation
  },
  data() {
    return {
      dialogFormVisible: false,
      action: '',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      fileName: '移交服务周报' + formatDate(new Date()),
      importExcelUrl: importExcelUrl,
      uploadDialogFormVisible: false,
      template: template
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.$parent.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    // 取消
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    // 导出
    exportExcel() {
      exportExcel().then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = this.fileName + '.xlsx'
        // not IE downLoad
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href)
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    handleError(data) {
      this.$message({
        showClose: true,
        message: data,
        type: 'error'
      })
    },
    handleSuccess(response) {
      this.uploadDialogFormVisible = false
      this.$message({
        showClose: true,
        message: '导入成功',
        type: 'success'
      })
      this.$parent.refresh()
    },
    closeDialog() {
      this.uploadDialogFormVisible = false
      this.$parent.refresh()
    }
  }
}
</script>
